package com.cloudcc.frame.trigger;

import com.cloudcc.deveditor.trigger.TriggerEditor;
import com.cloudcc.deveditor.core.CCObject;
import net.sf.json.JSONObject;

import java.util.List;
public class RetrieveApply_Test extends TriggerEditor {
    /*
     * @Description:
     * 1获取基本属性
     * 2判断是否已完成并且最新状态是已完成
     * 3验证是否已经有管户经理（兜底）
     * 4获取以前的管户小组数据
     * 5插入管护小组，判断当前客户的管户人是否公海池：
     * a:是的话领取
     * b:否的话共享
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public void afterupdate(){
    String ghzt_continue="已失效";
    String ghzt_start="未失效";
    String YWC="审批成功";
    String YES="是";
    String NO="否";
    String spzt_old=record_old.get("spzt")==null?"":record_old.get("spzt")+"";
    String spzt_new=record_new.get("spzt")==null?"":record_new.get("spzt")+"";
    //交易中心
    String tradingCenter=record_new.get("tradingCenter")==null?"":record_new.get("tradingCenter")+"";
    String code=record_new.get("code")==null?"":record_new.get("code")+"";
    String sql="select id from Account where invoiceTaxNo='"+code+"' and is_deleted='0'";
    //客户
    String fk_account="";
        List<CCObject> account1 = this.cqlQuery("Account", sql);
        if(account1.size()>0){
            fk_account=account1.get(0).get("id")==null?"":account1.get(0).get("id")+"";
        }
        //所有人
    String ownerid=record_new.get("ownerid")==null?"":record_new.get("ownerid")+"";
    if(spzt_new.equals(YWC)&&(!spzt_new.equals(spzt_old))){
        String check_sql="select id from manage_group " +
                "where fk_psmc='"+tradingCenter+"' " +
                "and is_deleted='0' " +
                "and ghzt!='"+ghzt_continue+"' " +
                "and fk_account='"+fk_account+"'";
        List<CCObject> manage_group_list = this.cqlQuery("manage_group", check_sql);
        if(manage_group_list.size()>0){
            if(true)trigger.addErrorMessage("当前交易中心已存在管户经理，不允许拾取");
        }else{
            String invoiceTaxNo="";
            String ghr="";
            String code_sql="select invoiceTaxNo,ownerid from Account where id='"+fk_account+"' and is_deleted='0'";
            List<CCObject> account = this.cqlQuery("Account", code_sql);
            if(account.size()>0){
                 invoiceTaxNo = account.get(0).get("invoiceTaxNo") == null ? "" : account.get(0).get("invoiceTaxNo") + "";
                ghr = account.get(0).get("ownerid") == null ? "" : account.get(0).get("ownerid") + "";
            }
            String get_manager="select id from manage_group " +
                    "where fk_ccuser='"+ownerid+"' " +
                    "and is_deleted='0' " +
                    "and fk_account='"+fk_account+"' " +
                    "and ghzt='"+ghzt_continue+"'";
            List<CCObject> managers = this.cqlQuery("manage_group", get_manager);
            String fk_city_name1="";
            String fk_country_name1="";
            String lxdz="";
            String dlwzco="";//经度
            String dlwzla="";//纬度
            String province1="";
            if(managers.size()>0){
                fk_city_name1= managers.get(0).get("fk_city_name1")==null?"":managers.get(0).get("fk_city_name1")+"";
                fk_country_name1= managers.get(0).get("fk_country_name1")==null?"":managers.get(0).get("fk_country_name1")+"";
                lxdz= managers.get(0).get("lxdz")==null?"":managers.get(0).get("lxdz")+"";
                dlwzco= managers.get(0).get("dlwzco")==null?"":managers.get(0).get("dlwzco")+"";
                dlwzla= managers.get(0).get("dlwzla")==null?"":managers.get(0).get("dlwzla")+"";
                province1= managers.get(0).get("province1")==null?"":managers.get(0).get("province1")+"";
            }
            CCObject manage_group = new CCObject("manage_group");
            manage_group.put("fk_ccuser",ownerid);
            manage_group.put("name",invoiceTaxNo);
            manage_group.put("ghzt",ghzt_start);
            manage_group.put("fk_city_name1",fk_city_name1);
            manage_group.put("fk_country_name1",fk_country_name1);
            manage_group.put("lxdz",lxdz);
            manage_group.put("dlwzco",dlwzco);
            manage_group.put("dlwzla",dlwzla);
            manage_group.put("province1",province1);
            List<CCObject> ccuser = this.cquery("ccuser", "id='" + ghr + "'");
            if(ccuser.size()>0){
               String is_pool= ccuser.get(0).get("sfdzbhghc")==null?"":ccuser.get(0).get("sfdzbhghc")+"";
               if(is_pool.equals(YES)){
                   //领取
                   manage_group.put("is_shared",NO);
               }else{
                   //共享
                   manage_group.put("is_shared",YES);
               }
            }
            this.insert(manage_group);
        }
    }
    }

    public void beforeinsert(){
        //定义关联客户id
        String account_id = "";
        //拿到统一社会信用代码
        String code = record_new.get("code")==null?"":record_new.get("code")+"";
        //根据code查找客户的id
        String sql = "select id from account " +
                "where invoiceTaxNo='"+code+"' " +
                "and is_deleted='0'";
        List<CCObject> account_list = this.cqlQuery("account", sql);
        //存在
        if (account_list.size()>0){
            //拿到客户id
            account_id = account_list.get(0).get("id")==null?"":account_list.get(0).get("id")+"";
            //电子保函管户权拾回申请
            CCObject retrieveApply = new CCObject("retrieveApply");
            retrieveApply.put("fk_account",account_id);
            //插入关联客户id
            this.insert(retrieveApply);
        }
    }
}
